<%@ page contentType="text/html; charset=utf-8"%>
<%@ include file="/WEB-INF/jsp/common/tags.jsp"%>
<aos:html>
<aos:head title="代码生成器">
	<aos:include lib="ext" />
	<aos:base href="system/coder" />
</aos:head>
<aos:body>
</aos:body>
<aos:onready>
	<aos:viewport layout="border">
		<aos:treepanel id="_t_project" region="west" bodyBorder="0 1 0 0" width="300" singleClick="true"
			url="listProjectTree.jhtml" nodeParam="path" rootId="aosroot_" rootText="${app_name_ }" rootIcon="icon25.gif"
			header="false" collapsible="true" collapseMode="mini" split="true">
			<aos:docked forceBoder="0 1 1 0">
				<aos:dockeditem xtype="tbtext" text="项目结构视图" />
			</aos:docked>
			<aos:menu>
				<aos:menuitem text="新建JSP视图" disabled="false" onclick="_w_jsp_show" icon="bullet_blue.png" />
				<aos:menuitem text="新建Controller控制器类" disabled="false" onclick="_w_c_show" icon="bullet_blue.png" />
				<aos:menuitem text="新建Service服务类" disabled="false"  onclick="_w_s_show" icon="bullet_blue.png" />
				<aos:menuitem text="新增模块向导" disabled="true" icon="modules_wizard.gif" />
				<aos:menuitem xtype="menuseparator" />
				<aos:menuitem text="新建SQL VarMapper映射" disabled="false"  onclick="_w_sqlmapper_show"  icon="bullet_red.png" />
				<aos:menuitem text="新建VO值对象类" disabled="true" icon="bullet_red.png" />
				<aos:menuitem text="批量生成Dao层代码" onclick="_w_dao_show" icon="bullet_group.gif" />
				<aos:menuitem xtype="menuseparator" />
				<aos:menuitem text="刷新" onclick="_t_project_refresh" icon="refresh.png" />
			</aos:menu>
		</aos:treepanel>

		<aos:iframe region="center" loadMask="${page_load_msg_ }" src="fileInit.jhtml" />

		<aos:window id="_w_dao" title="批量生成Dao层代码" width="900" height="-10" layout="fit" onshow="_w_dao_onshow">
			<aos:gridpanel id="_g_dao" bodyBorder="1 0 0 0" url="listTables.jhtml" hidePagebar="true"
				onselectionchange="_g_dao_sc">
				<aos:docked forceBoder="0 0 1 0">
					<aos:textfield id="_name" width="150" emptyText="数据表名称" onenterkey="_g_dao_query" />
					<aos:dockeditem xtype="tbtext" text="作者:" />
					<aos:textfield id="author" width="160" readOnly="false"  value="${user_name}"/>
					<aos:dockeditem xtype="tbtext" text="Dao包路径:" />
					<aos:textfield id="_dao_pkg" width="160" readOnly="true" />
					<aos:dockeditem xtype="tbtext" text="已选表: " />
					<aos:textfield id="_selection" value="0" width="40" readOnly="true" />
					<aos:dockeditem xtype="tbfill" />
					<aos:dockeditem text="导入表结构" icon="go.gif" onclick="fn_import_dll" />
					<aos:dockeditem text="生成代码" icon="go.gif" onclick="fn_build_dao" />
				</aos:docked>
				<aos:selmodel type="checkbox" mode="simple" />
				<aos:column type="rowno" />
				<aos:column header="表名" dataIndex="name" width="220" />
				<aos:column header="表注释" dataIndex="comment" flex="1" />
				<aos:column header="宿主" dataIndex="owner" width="120" />
			</aos:gridpanel>
			<aos:docked dock="bottom" ui="footer">
                <aos:dockeditem xtype="tbtext" iconVec="fa-hand-o-right " text=" 提示：请先到[字典参数->平台配置]模块中配置数据库的连接参数：websql_jdbc_。" />
				<aos:dockeditem xtype="tbfill" />
				<aos:dockeditem onclick="#_w_dao.hide();" text="关闭" icon="close.png" />
			</aos:docked>
		</aos:window>
		
		
		<aos:window id="_w_vc" title="批量生成VC层代码" width="1000" height="-10" layout="fit" onshow="_w_vc_onshow">
			<aos:gridpanel id="_g_vc" bodyBorder="1 0 0 0" url="../../autobuilder/builder/listBuilder_table.jhtml" hidePagebar="true"
				onselectionchange="_g_vc_sc">
				<aos:docked forceBoder="0 0 1 0">
					<!-- <aos:textfield id="_name2" width="150" emptyText="数据表名称" onenterkey="_g_vc_query" /> -->
					<aos:dockeditem xtype="tbtext" text="作者:" />
					<aos:textfield id="author2" width="160" readOnly="false" value="${user_name}" />
					<aos:dockeditem  id="controlnametext"  xtype="tbtext" text="controlname" />
					<aos:textfield id="controlname" width="120" readOnly="false" />
					<aos:dockeditem  id="jsp_path_text"  xtype="tbtext" text="JSP输出路径位置" />
					<aos:textfield id="jsp_path_" width="130" readOnly="false" />
					<aos:dockeditem xtype="tbtext" text="已选表: " />
					<aos:textfield id="_selection_vc" value="0" width="40" readOnly="true" />
					<aos:dockeditem xtype="tbfill" />
					<aos:dockeditem text="生成代码" icon="go.gif" onclick="fn_build_" />
				</aos:docked>
					<aos:docked forceBoder="0 0 1 0">
					 <aos:dockeditem xtype="tbtext"  text="Dao包路径:" />
					<aos:textfield id="_pkg" width="320" readOnly="true" />
				</aos:docked>
				<aos:selmodel type="checkbox" mode="multi" />
				<aos:column type="rowno" />
				<aos:column header="ID" hidden="true"  dataIndex="id_"   width="60"  />
				<aos:column header="表名" dataIndex="table_name_" width="160" />
				<aos:column header="表描述" dataIndex="table_des_" flex="1" width="160" />
				<aos:column header="daopackage_" dataIndex="daopackage_" width="120" />
				<aos:column header="servicepackage_" dataIndex="servicepackage_" width="120" />
				<aos:column header="controllername_" dataIndex="controllername_" width="120" />
				<aos:column header="sqlmapper_" dataIndex="sqlmapper_" width="120" />
				<aos:column header="jsp_path_" dataIndex="jsp_path_" width="120" />
				<aos:column header="宿主" dataIndex="db_name_" width="120" />
			</aos:gridpanel>
			<aos:docked dock="bottom" ui="footer">
                <aos:dockeditem xtype="tbtext" iconVec="fa-hand-o-right " text=" 提示：请先到[字典参数->平台配置]模块中配置数据库的连接参数：websql_jdbc_。" />
				<aos:dockeditem xtype="tbfill" />
				<aos:dockeditem onclick="#_w_vc.hide();" text="关闭" icon="close.png" />
			</aos:docked>
		</aos:window>

	</aos:viewport>
	<script type="text/javascript">
	  //弹出Dao代码生成器窗口
	  var codetype="";
	  function _w_dao_show(){
		  var record = AOS.selectone(_t_project, false);
		  //!AOS.empty(record.raw.b) ||
		  if( record.raw.b == '1'){
			  _w_dao.show();
		  }else{
			  AOS.tip('选中节点不合法,请选择dao节点，请重新选择。');
		  }
	  }
	  
	   //监听代码生成器窗口弹出事件
	   function _w_dao_onshow(){
		   var record = AOS.selectone(_t_project, false);
		   _dao_pkg.setValue(record.raw.c);
		   _g_dao_query();
	   }
	
		//查询数据表
		function _g_dao_query() {
			var params = {
					name_ : _name.getValue()
				};
			 _g_dao_store.getProxy().extraParams = params;
			 _g_dao_store.loadPage(1);
		}
		
		//数据表的selectionchange事件
		function _g_dao_sc(){
			_selection.setValue(AOS.rows(_g_dao));
		}

		//刷新工程结构树
		function _t_project_refresh() {
			var refreshnode = AOS.selectone(_t_project);
			if (!refreshnode) {
				refreshnode = _t_project.getRootNode();
			}
			if (refreshnode.isLeaf()) {
				refreshnode = refreshnode.parentNode;
			}
			var params = {};
			_t_project_store.getProxy().extraParams = params;
			_t_project_store.load({
				node : refreshnode,
				callback : function() {
					refreshnode.collapse();
					refreshnode.expand();
				}
			});
		}
		 //生成Dao代码
		function fn_build_dao(){
			 var rows = AOS.rows(_g_dao);
			 if(rows === 0){
				 AOS.tip('请先选择数据表。');
				 return;
			 }
             var msg = AOS.merge('提示：确认生成选中的[{0}]张数据表对应的Dao层代码吗？', rows);
             AOS.confirm(msg, function (btn) {
                 if (btn === 'cancel') {
                     AOS.tip('操作被取消。');
                     return;
                 }
	  			   var record = AOS.selectone(_t_project, false);
					AOS.ajax({
					params:{
						aos_rows_: AOS.selection(_g_dao, 'name'),
						outPath : record.data.id,
						package_: record.raw.c,
						author:author.getValue(),
						db_name_:AOS.selection(_g_dao, 'owner')
					},
					url : 'buildDaoCode.jhtml',
					ok : function(data) {
						AOS.tip(data.appmsg);
						_w_dao.hide();
						_t_project_refresh();
					}
				});
             });
			 
		}
		 // 选择事件
		function _g_vc_sc(){
			_selection_vc.setValue(AOS.rows(_g_vc));
			var record = AOS.selectone(_g_vc, true);
			if(!AOS.empty(record) ){
				var u_jsppath="";var u_controlpath = "";
				var daopackage_=record.raw.daopackage_;
				if(daopackage_.lastIndexOf(".")>-1){
				var path_user = daopackage_.substring(0,daopackage_.lastIndexOf("."));
				u_jsppath =  path_user.substring(path_user.lastIndexOf(".")+1,path_user.length); //默认作为JSP路径
					if(path_user.lastIndexOf(".")>-1){
						var path_user_controll = path_user.substring(0,path_user.lastIndexOf("."));
						u_controlpath =  path_user_controll.substring(path_user_controll.lastIndexOf(".")+1,path_user_controll.length); //默认作为Controller路径
						 
					}
				}
				var controllername_=record.raw.controllername_;
				 if(AOS.empty(controllername_)){ //如果为空
					 if(!AOS.empty(u_controlpath)){
						 controlname.setValue(u_controlpath); 
					 }
				 }else{
					controlname.setValue(record.raw.controllername_); 
				 }
			    var jsp_path_o=record.raw.jsp_path_;
                if(AOS.empty(jsp_path_o)){ //如果为空
                	 if(!AOS.empty(u_controlpath) && !AOS.empty(u_jsppath)){
                		 jsp_path_.setValue(u_controlpath+"/"+u_jsppath); 
					 }
				 }else{
					 jsp_path_.setValue(record.raw.jsp_path_);
				 }
				
			}
			
		}
		 //查询 
		function _g_vc_query() {
			var params = {
					name_ : _name.getValue()
				};
			_g_vc_store.getProxy().extraParams = params;
			_g_vc_store.loadPage(1);
		}
		function _w_c_show(){
			  var record = AOS.selectone(_t_project, false);
			  if( record.raw.b == '3'){ //!AOS.empty(record.raw.b) ||
				  codetype = "Controller";
				  _w_vc.show();
			  }else{
				  AOS.tip('选中节点不合法,请选择controller节点，请重新选择。');
			  }
		  }
		//监听VC窗口弹出事件
	    function _w_vc_onshow(){
			   var record = AOS.selectone(_t_project, false);
			   _pkg.setValue(record.raw.c);
			   _g_vc_query();
		 }
		//生在Service窗口
	    function _w_s_show(){
			  var record = AOS.selectone(_t_project, false);
			  if( record.raw.b == '2'){ //!AOS.empty(record.raw.b) ||
				   codetype = "Service";
				  _w_vc.show();
			  }else{
				  AOS.tip('选中节点不合法service节点，请重新选择。');
			  }
		  }
	    //生在JSP窗口
	    function _w_jsp_show(){
			  var record = AOS.selectone(_t_project, false);
			  if( record.raw.b == '4'){ //!AOS.empty(record.raw.b) ||
				   codetype = "JSP";
				  _w_vc.show();
			  }else{
				  AOS.tip('选中节点不合法JSP节点，请重新选择。');
			  }
		  }
	    
	   //生在_w_sqlmapper窗口
	    function _w_sqlmapper_show(){
			  var record = AOS.selectone(_t_project, false);
			  if( record.raw.b == '1'){ //!AOS.empty(record.raw.b) ||
				   codetype = "SqlMapper";
				   _w_vc.show();
			  }else{
				  AOS.tip('选中节点不合法dao节点，请重新选择。');
			  }
		  }
	    
		//生成代码
		function fn_build_(){
			var rows = AOS.rows(_g_vc);
			if(rows === 0){
				 AOS.tip('请先选择数据表。');
				 return;
			}
            var msg = AOS.merge('提示：确认生成选中的[{0}]张数据表对应的service层代码吗？', rows);
            AOS.confirm(msg, function (btn) {
                if (btn === 'cancel') {
                    AOS.tip('操作被取消。');
                    return;
                }
                if(codetype== "Controller"){
                	if(AOS.empty(controlname.getValue())){
                		AOS.tip('Controllername不能为空。');
                		return;
                	}
                	if(AOS.empty(jsp_path_.getValue())){
                		AOS.tip('jsp_path_不能为空。');
                		return;
                	}
                }
  			   var record = AOS.selectone(_t_project, false);
				AOS.ajax({
				params:{
					aos_rows_: AOS.selection(_g_vc, 'table_name_'),
					outPath : record.data.id,
					package_: record.raw.c,
					author:author.getValue(),
					db_name_:AOS.selection(_g_vc, 'db_name_'),
					daopackage_:AOS.selection(_g_vc, 'daopackage_'),
					servicepackage_:AOS.selection(_g_vc, 'servicepackage_'),
					controllername_:AOS.selection(_g_vc, 'controllername_'),
					sqlmapper_:AOS.selection(_g_vc, 'sqlmapper_'),
					jsp_path_:jsp_path_.getValue(),
					id_:AOS.selection(_g_vc, 'id_'),
					codetype:codetype,
					controlname:controlname.getValue()
				},
				url : 'buildCode.jhtml',
				ok : function(data) {
					AOS.tip(data.appmsg);
					_w_vc.hide();
					_t_project_refresh();
				}
			});
           });	 
		}
		function fn_import_dll(){
			 var rows = AOS.rows(_g_dao);
			 if(rows === 0){
				 AOS.tip('请先选择数据表。');
				 return;
			 }
             var msg = AOS.merge('提示：确认导入选中的[{0}]到库？', rows);
             AOS.confirm(msg, function (btn) {
                 if (btn === 'cancel') {
                     AOS.tip('操作被取消。');
                     return;
                 }
	  			   var record = AOS.selectone(_t_project, false);
					AOS.ajax({
					params:{
						aos_rows_: AOS.selection(_g_dao, 'name'),
						outPath : record.data.id,
						package_: record.raw.c,
						author:author2.getValue(),
						table_name_:AOS.selection(_g_dao, 'name'),
						db_name_:AOS.selection(_g_dao, 'owner')
					},
					url : '../../autobuilder/builder/importtableinfos.jhtml',
					ok : function(data) {
						AOS.tip(data.appmsg);
						//_w_dao.hide();
						//_t_project_refresh();
					}
				});
             });
		}
	</script>
</aos:onready>
</aos:html>